﻿Public Class Login

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Application.Exit()
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        ' Ensure the Username and Password textboxes have valid information
        If Me.txtUserName.Text = "" Then
            MessageBox.Show("Login Error: Please enter a Username.", "Missing credentials", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf Me.txtPassword.Text = "" Then
            MessageBox.Show("Login Error: Please enter a Password.", "Missing credentials", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf Me.txtUserName.Text.Contains("'") Then
            MessageBox.Show("Login Error: Really trying to use SQL Injection? Come on bro...", "Nice try!", MessageBoxButtons.OK, MessageBoxIcon.Question)
        Else
            ' Create a user and check password credential for validation
            Dim newUser As New clsUser(Me.txtUserName.Text)

            Select Case newUser.Authenticate(Me.txtPassword.Text)
                Case AuthResult.AUTH_OK
                    clsUIBase.AuthenticatedUser = newUser
                    ' User exists, has the correct credentials, and is a tech, continue to the Dashboard
                    DialogResult = DialogResult.OK
                Case AuthResult.NOT_TECH
                    ' User exists, has the correct credentials, but is not a tech
                    MessageBox.Show("Login Error: This account is not set up as a technician. Please contact your system administrator.", _
                                "Insufficient Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Case AuthResult.UNAUTH
                    ' User has entered their username, password, or both incorrectly
                    MessageBox.Show("Login Error: Your Username and/or Password are not valid. Please try again or contact your system administrator.", _
                                "Login Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Select
        End If
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ' Clear current values from textboxes
        txtUserName.Text = ""
        txtPassword.Text = ""

        ' Set the focus back to the Username textbox
        Me.txtUserName.Focus()
    End Sub
End Class
